using DG.Tweening;
using UnityEngine;

public class PlayerControle : MonoBehaviour
{
    public QuiQuiObj m_QuiQui;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (!m_QuiQui)
            return;

#if UNITY_STANDALONE_WIN
        // move
        Vector2 moveDir = Vector2.zero;
        if (Input.GetKey(KeyCode.W))
            moveDir = Vector2.up;
        else if (Input.GetKey(KeyCode.S))
            moveDir = Vector2.down;

        if (Input.GetKey(KeyCode.A))
            moveDir += Vector2.left;
        else if (Input.GetKey(KeyCode.D))
            moveDir += Vector2.right;
        Move(moveDir);
#endif

        // intersection
        if (Input.GetKeyDown(KeyCode.F))
            DoIntersection();

        if (Input.GetKeyDown(KeyCode.Q))
            DoTake();

        if (Input.GetKeyDown(KeyCode.Tab))
            DoMap();

        DoAttack(Input.GetKey(KeyCode.Space));

        if (Input.GetKeyDown(KeyCode.L))
        {
            MessageShow.Instance.Message("���Բ��Բ��Բ�������˹��������");
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            var bigFood = IntersectionObjManager.Instance.CreateObj(IntersectionObjManager.IntersectionObjType.BigFood);
            bigFood.transform.position = transform.position;
            bigFood.transform.DOMove(transform.position + new Vector3(1, 1, 0), 0.5f);
        }

    }

    public void Move(Vector2 moveDir)
    {
        if (!m_QuiQui)
            return;
        if (moveDir.x != 0 || moveDir.y != 0)
        {
            m_QuiQui.Move(moveDir.normalized);
        }
        else
        {
            m_QuiQui.Stand();
            Debug.Log("stand");
        }
    }

    public void DoTake()
    {
        if (!m_QuiQui.takingObj)
            m_QuiQui.DoTake();
        else
            m_QuiQui.DoPlace();
    }
    public void DoMap()
    {
        WorldManager.Instance.SetGlobalView(!WorldManager.Instance.isGlobalView);
    }

    public void DoIntersection()
    {
        
        m_QuiQui.DoIntersection();
    }

    public void DoAttack(bool isAttack)
    {
        if(m_QuiQui)
            m_QuiQui.isAttacking = isAttack;
    }
    
    private void FixedUpdate()
    {
        if (!m_QuiQui)
        {
            m_QuiQui = GetComponent<QuiQuiObj>();

        }
        if (m_QuiQui.hungryVaue < 20)
        {
            var warningPanel = UIManager.Instance.OpenUI(UIManager.UIEnum.WarningRoundPanel) as WarningRoundPanel;
            warningPanel.transparent = 1f - m_QuiQui.hungryVaue / 20;
        }
        else
        {
            UIManager.Instance.CloseUI(UIManager.UIEnum.WarningRoundPanel);
        }
#if UNITY_ANDROID
        var androidOperationUI = UIManager.Instance.OpenUI(UIManager.UIEnum.AndroidOperationUI) as AndroidOperationUI;
#endif

        bool hasFoucsObj = false;
        for (int i = 0; i < m_QuiQui.foucsObjects.Count; ++i)
        {
            var intersectionObj = m_QuiQui.foucsObjects[i];
            
            // check intersectionObj
            if (intersectionObj.gameObject.activeSelf && intersectionObj.isSupportIntersection && !m_QuiQui.intersectingObj && intersectionObj.CanIntersection())
            {
                hasFoucsObj = true;
                var hotKeyTipsPanel = UIManager.Instance.OpenUI(UIManager.UIEnum.HotKeyTipsPanel) as HotkeyTipsPanel;
                hotKeyTipsPanel.ShowTip(intersectionObj.gameObject, HotkeyTipsPanel.FloatingType.PressF, intersectionObj.gameObject.name);
#if UNITY_ANDROID
                androidOperationUI.SetIntersectioinBInteractable(true);
#endif
            }
            
            if (intersectionObj.gameObject.activeSelf && intersectionObj.isSupportTake)
            {
                hasFoucsObj = true;
                var hotKeyTipsPanel = UIManager.Instance.OpenUI(UIManager.UIEnum.HotKeyTipsPanel) as HotkeyTipsPanel;
                hotKeyTipsPanel.ShowTip(intersectionObj.gameObject, HotkeyTipsPanel.FloatingType.PressF, intersectionObj.gameObject.name);
#if UNITY_ANDROID
                androidOperationUI.SetTakeBInteractable(true);
#endif
            }
        }
        if (!hasFoucsObj || m_QuiQui.intersectingObj)
        {
            var hotKeyTipsPanel = UIManager.Instance.OpenUI(UIManager.UIEnum.HotKeyTipsPanel) as HotkeyTipsPanel;
            hotKeyTipsPanel.HideTip(HotkeyTipsPanel.FloatingType.PressF);
#if UNITY_ANDROID
            androidOperationUI.SetIntersectioinBInteractable(false);
#endif
        }
#if UNITY_ANDROID
        if(m_QuiQui.takingObj)
            androidOperationUI.SetTakeBInteractable(true);
#endif

    }
}
